using System;
using System.Windows.Forms;
using TXLooker.WorkItemExtention.UI.QueryManager;

namespace TXLooker.WorkItemExtention.UI
{
    #region EditControl
    public partial class ComboBoxEdit : UserControl, IDataGridViewEditingControl
    {
        #region private variables

        private bool m_valueChanged = false;
        private DataGridView m_dataGrid;

        #endregion

        public ComboBoxEdit()
        {
            InitializeComponent();
        }

        private void _SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnValueChanged();    
        }

        private Query m_querySession;

        private void OnValueChanged()
        {
            this.m_valueChanged = true;
            this.m_querySession.QueryValue = (this.m_comboBoxEdit.SelectedItem == null ? string.Empty : this.m_comboBoxEdit.SelectedItem.ToString());
            DataGridView dgv = this.EditingControlDataGridView;
            if (dgv != null)
                dgv.NotifyCurrentCellDirty(true);
        }

        public void SetupControls(Query q)
        {
            this.m_querySession = new Query();
            this.m_querySession.QueryValue = q.QueryValue;
        }

        #region IDataGridViewEditingControl

        public object EditingControlFormattedValue
        {
            get
            {
                return this.m_querySession;
            }
            set
            {
                //nothing to do...
            }
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.m_comboBoxEdit.Font = dataGridViewCellStyle.Font;
            this.MinimumSize = this.Size;
        }

        private int m_row;

        public DataGridView EditingControlDataGridView
        {
            get { return this.m_dataGrid; }
            set { this.m_dataGrid = value; }
        }

        public int EditingControlRowIndex
        {
            get { return m_row; }
            set { m_row = value; }
        }

        public bool EditingControlValueChanged
        {
            get { return m_valueChanged; }
            set { m_valueChanged = value; }
        }

        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch (keyData & Keys.KeyCode)
            {
                case Keys.Down:
                case Keys.Right:
                case Keys.Left:
                case Keys.Up:
                case Keys.Home:
                case Keys.End:
                    return true;
                default:
                    return false;
            }
        }

        public Cursor EditingPanelCursor
        {
            get { return base.Cursor; }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.EditingControlFormattedValue;
        }

        public void PrepareEditingControlForEdit(bool selectAll) { }

        public bool RepositionEditingControlOnValueChange { get { return false; } }
        
        #endregion
    }
    #endregion
}
